#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = '云絮'
from libmproxy import controller, proxy
from libmproxy.proxy.server import ProxyServer
import util
import thread
from config import config

class StickyMaster(controller.Master):
    def __init__(self, server):
        controller.Master.__init__(self, server)
        self.stickyhosts = {}

    def run(self):
        try:
            return controller.Master.run(self)
        except KeyboardInterrupt:
            self.shutdown()

    def handle_request(self, flow):
        thread.start_new_thread(util.filter,(flow.request,))
        flow.reply()

    def handle_response(self, flow):
	    print flow.response.headers
	    flow.reply()


config = proxy.ProxyConfig(host=config['proxy_addr'],port=config['proxy_port'])
server = ProxyServer(config)
m = StickyMaster(server)
m.run()
